typedef int QDatatype;

typedef struct QueueNode
{
	struct QueueNode* next;
	QDatatype data;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;


void QueueInit(Queue* pq)
{
	assert(pq);

	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDatatype x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pq->head == NULL)
	{
		assert(pq->tail == NULL);

		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);

	/*QNode* next = pq->head->next;
	free(pq->head);
	pq->head = next;

	if (pq->head == NULL)
		pq->tail = NULL;*/

	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}

	pq->size--;
}

int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->size == 0;
}

QDatatype QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->data;
}

QDatatype QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->tail->data;
}


typedef struct {
	Queue* q1;
	Queue* q2;
} MyStack;


MyStack* myStackCreate() {
	MyStack* sta = (MyStack*)malloc(sizeof(MyStack));
	if (sta == NULL)
	{
		perror("malloc");
		return;
	}
	QueueInit(&sta->q1);
	QueueInit(&sta->q2);
	return sta;
}
bool myStackEmpty(MyStack* obj) {
	return QueueEmpty(&obj->q2) && QueueEmpty(&obj->q1);
}


void myStackPush(MyStack* obj, int x) {
	assert(obj);
	if (!QueueEmpty(&obj->q1))
	{
		QueuePush(&obj->q1, x);
	}
	else
	{
		QueuePush(&obj->q2, x);
	}
}

int myStackPop(MyStack* obj) {
	assert(obj);
	assert(!myStackEmpty(obj));
	Queue* emptyQ = &obj->q2;
	Queue* noemptyQ = &obj->q1;
	if (obj->q1 == NULL)
	{
		Queue* emptyQ = &obj->q1;
		Queue* noemptyQ = &obj->q2;
	}
	while (QueueSize(noemptyQ) > 1)
	{
		QueuePush(emptyQ, QueueFront(noemptyQ));
		QueuePop(noemptyQ);
	}
	int tem = QueueFront(noemptyQ);
	QueuePop(noemptyQ);
	return tem;
}

int myStackTop(MyStack* obj) {
	assert(obj);
	assert(!myStackEmpty(obj));
	Queue* emptyQ = obj->q2;
	Queue* noemptyQ->q1;
	if (obj->q1 == NULL)
	{
		Queue* emptyQ = obj->q1;
		Queue* noemptyQ->q2;
	}
	while (QueueSize(noemptyQ) > 1)
	{
		QueuePush(emptyQ, QueueFront(noemptyQ));
		QueuePop(noemptyQ);
	}
	int tem = QueueFront(noemptyQ);
	QueuePush(emptyQ, QueuePop(noemptyQ));
	return tem;

}

bool myStackEmpty(MyStack* obj) {
	return QueueEmpty(obj->q2) && QueueEmpty(obj->q1);
}

void myStackFree(MyStack* obj) {
	free(obj->q1);
	free(obj->q2);
	free(obj);
}

/**
 * Your MyStack struct will be instantiated and called as such:
 * MyStack* obj = myStackCreate();
 * myStackPush(obj, x);

 * int param_2 = myStackPop(obj);

 * int param_3 = myStackTop(obj);

 * bool param_4 = myStackEmpty(obj);

 * myStackFree(obj);
*/